# -*- coding: utf-8 -*-
"""
@Author: Logan
@Date:   2025/6/30 18:31
@Description: 
"""
from time import sleep

from base.GoodsBase import GoodsBase
from selenium.webdriver.common.by import By
from base.ObjectMap import ObjectMap
from common.tools import get_img_path


class GoodsPage(GoodsBase, ObjectMap):

    def input_goods_textarea(self, driver, placeholder, input_value):
        """
        输入商品文本域
        :param driver:
        :param placeholder:
        :param input_value:
        :return:
        """
        goods_textarea_xpath = self.goods_textarea(placeholder)
        return self.element_fill_value(driver, By.XPATH, goods_textarea_xpath, input_value)

    def change_goods_num(self, driver, num, plus=True):
        """
        输入商品数量
        :param driver:
        :param num:
        :param plus:
        :return:
        """
        if plus:  # 使用加号添加商品
            add_goods_num_xpath = self.goods_num(plus)
            # 循环点击num次
            for i in range(num):
                self.element_click(driver, By.XPATH, add_goods_num_xpath)
                sleep(0.5)
        else:
            input_goods_num_xpath = self.goods_num(plus)
            self.element_fill_value(driver, By.XPATH, input_goods_num_xpath, num)

    def upload_goods_image(self, driver, image_name):
        """
        上传商品图片
        :param driver:
        :param image_name:
        :return:
        """
        image_path = get_img_path(image_name)
        goods_image_xpath = self.goods_img()
        return self.upload(driver, By.XPATH, goods_image_xpath, image_path)

    def input_goods_price(self, driver, price):
        """
        输入商品数量
        :param driver:
        :param price:
        :return:
        """
        goods_price_xpath = self.goods_price()
        return self.element_fill_value(driver, By.XPATH, goods_price_xpath, price)

    def select_goods_status(self, driver, status):
        """
        选择商品状态
        :param driver:
        :param status:
        :return:
        """
        # 先点击选择框
        goods_status_xpath = self.goods_status()
        self.element_click(driver, By.XPATH, goods_status_xpath)
        sleep(1)
        select_goods_status_xpath = self.goods_status_select(status)
        return self.element_click(driver, By.XPATH, select_goods_status_xpath)

    def click_bottom_button(self, driver, button_name):
        """
        底部按钮
        :param driver:
        :param button_name:
        :return:
        """
        click_button_xpath = self.add_goods_bottom_button(button_name)
        return self.element_click(driver, By.XPATH, click_button_xpath)

    def add_new_goods(self, driver, goods_title, goods_details, goods_num, goods_pic_list, goods_price, goods_status,
                      bottom_button_name):
        """
        添加商品
        :param driver:
        :param goods_title:
        :param goods_details:
        :param goods_num:
        :param goods_pic_list:
        :param goods_price:
        :param goods_status:
        :param bottom_button_name:
        :return:
        """
        self.input_goods_textarea(driver, "标题", goods_title)
        self.input_goods_textarea(driver, "详情", goods_details)
        self.change_goods_num(driver, goods_num, plus=True)
        self.change_goods_num(driver, goods_num, plus=False)
        for img in goods_pic_list:
            self.upload_goods_image(driver, img)
            sleep(5)
        self.input_goods_price(driver, goods_price)
        self.select_goods_status(driver, goods_status)
        self.click_bottom_button(driver, bottom_button_name)
